﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class FloorUI : Form
    {
        // note to self - add speed and substitute for thread sleep -aDRIAN
        
        int floorNumber;
        Elevator elevator;
        //MonitoringUI mui; 

        // Just like the 'final' keyword in Java
        readonly String DOORS_OPEN = "open";
        readonly String DOORS_CLOSED = "closed";
        readonly String MOVING_DIR_DOWN = "moving down";
        readonly String MOVING_DIR_UP = "moving up";
        readonly String MOVING_DIR_STATIONARY = "stationary";
        readonly String MAX_FLOOR_MSG = "You are already on the top floor. You can't go any higher.";
        readonly String MIN_FLOOR_MSG = "You are already on the bottom floor. You can't go any lower.";
  
        public FloorUI(Elevator e, int floorNum)
        {
            InitializeComponent();
            floorNumber = floorNum;
            elevator = e;
            //mui = monoUI; 
            currentFloorLbl1.Text = "" + e.getCurrentFloor();
            currentFloorLbl1.Text = "" + floorNumber;
            
            if (floorNumber == elevator.getMaxFloor())
            {
                upBttn1.Enabled = false;
            }
            if (floorNumber == elevator.getMinFloor())
            {
                downBttn1.Enabled = false; 
            }
        }

        private void FloorUI_Load(object sender, EventArgs e)
        {
            //new UCC.UCCUpdateMonitoringUI(mui).Run();
            elevatorImgBox1.Image = Properties.Resources.doors_closed_small;
        }

        private void upBttn1_Click(object sender, EventArgs e)
        {
            upBttn1.Image = Properties.Resources.up_button_small_lit;
            if (floorNumber == elevator.getMaxFloor())
            {
                MessageBox.Show(MAX_FLOOR_MSG);
            }
            else if (elevator.getCurrentFloor() == floorNumber)
            {
                Thread.Sleep(500);
                elevatorImgBox1.Image = Properties.Resources.doors_open_small;
                elevator.setDoors(DOORS_OPEN);
                //new UCC.UCCUpdateMonitoringUI(mui).Run();
                enterBttn1.Show();
            }
            else
            {
                int counter = elevator.getCurrentFloor();
                bool testCondition = true;
                while (testCondition)
                {
                    if (elevator.getCurrentFloor() > floorNumber)
                    {
                        elevator.setMovingDirection(MOVING_DIR_DOWN);
                        counter -= 1;
                        elevator.setCurrentFloor(counter);

                    }
                    else if (elevator.getCurrentFloor() < floorNumber)
                    {
                        elevator.setMovingDirection(MOVING_DIR_UP);
                        counter += 1;
                        elevator.setCurrentFloor(counter);
                    }
                    else
                    {
                        testCondition = false;
                        Thread.Sleep(500);
                        elevatorImgBox1.Image = Properties.Resources.doors_open_small;
                        elevator.setDoors(DOORS_OPEN);
                        elevator.setMovingDirection(MOVING_DIR_STATIONARY);
                        //new UCC.UCCUpdateMonitoringUI(mui).Run();
                        enterBttn1.Show();
                    }
                    Thread.Sleep(500);
                    currentFloorLbl1.Text = "" + counter;
                    //new UCC.UCCUpdateMonitoringUI(mui).Run();
                    Application.DoEvents();
                }
            }
        }

        private void downBttn1_Click(object sender, EventArgs e)
        {
            downBttn1.Image = Properties.Resources.down_button_small_lit;
            if (floorNumber == elevator.getMinFloor())
            {
                MessageBox.Show(MIN_FLOOR_MSG);
            }
            else if (elevator.getCurrentFloor() == floorNumber)
            {
                Thread.Sleep(500);
                elevatorImgBox1.Image = Properties.Resources.doors_open_small;
                enterBttn1.Show();
            }
            else
            {
                int counter = elevator.getCurrentFloor();
                bool testCondition = true;
                while (testCondition)
                {
                    if (elevator.getCurrentFloor() > floorNumber)
                    {
                        counter -= 1;
                        elevator.setCurrentFloor(counter);
                    }
                    else if (elevator.getCurrentFloor() < floorNumber)
                    {
                        counter += 1;
                        elevator.setCurrentFloor(counter);
                    }
                    else
                    {
                        testCondition = false;
                        Thread.Sleep(500);
                        elevatorImgBox1.Image = Properties.Resources.doors_open_small;
                        enterBttn1.Show();
                    }
                    Thread.Sleep(500);
                    currentFloorLbl1.Text = "" + counter;
                    Application.DoEvents();
                }
            }
        }

        private void enterBttn1_Click(object sender, EventArgs e)
        {
            this.Hide();
            elevator.setDoors(DOORS_CLOSED);
            //new UCC.UCCUpdateMonitoringUI(mui).Run();
            //new UCC.UCCEnterElevator(elevator, elevator.getMinFloor(), elevator.getMaxFloor(), mui).Run();
        }
    }
}
